import info.gridworld.actor.Actor;
import info.gridworld.actor.Critter;
import info.gridworld.grid.Location;

import java.awt.Color;
import java.util.ArrayList;

/**
 * A <code>BlusterCritter</code> brighter or darker because of neighboring actors as
 * it moves through the grid. <br />
 * The implementation of this class is testable on the AP CS A and AB exams.
 */
public class BlusterCritter extends Critter
{
	private int c;
	private static final int DARKEN_FACTOR = -1;
	private static final int LIGHTEN_FACTOR = 1;
	/**
	 * the constructor of BlusterCritter
	 * @param c the the courage of the critter
	 */
	public BlusterCritter(int c)
	{
		super();
		this.c = c;
	}
	
	/**
	 * the same as flowers but more quickly
	 */
	private void darken()
    {
    	Color c = getColor();
    	int r = c.getRed();
    	int b = c.getBlue();
    	int g = c.getGreen();
    	if (r > 0) {
    		r = r + DARKEN_FACTOR;
    	}
    		
    	if (b > 0) {
    		b = b + DARKEN_FACTOR;
    	}
    		
    	if (g > 0) {
    		g = g + DARKEN_FACTOR;
    	}
    		
    	setColor(new Color(r, g, b));
    	
    }
	
	/**
	 * the same as darken
	 */
	private void lighten()
    {
    	Color c = getColor();
    	
    	int r = c.getRed();
    	int b = c.getBlue();
    	int g = c.getGreen();
    	if (r < 255) {
    		r = r + LIGHTEN_FACTOR;
    	}
    		
    	if (b < 255) {
    		b = b + LIGHTEN_FACTOR;
    	}
    		
    	if (g < 255) {
    		g = g + LIGHTEN_FACTOR;
    	}
    		
    	setColor(new Color(r, g, b));
    	
    }
	
    /**
     * If there are fewer than c critters, the BlusterCritter��s color gets brighter (color values increase). 
     * If there are c or more critters, the BlusterCritter��s color darken (color values decrease). 
     * @param actors the actors to be processed
     */
    public void processActors(ArrayList<Actor> actors)
    {
        int count = 0;
        for (Actor a : actors)
        {
        	if (a instanceof Critter)
        	{
        		count++;
        	}
        }
        if (count < c)
        {
        	lighten();
        }
        else
        {
        	darken();
        }
    }
    /**
     * get the actors around the BlusterCritter in two steps
     */
    public ArrayList<Actor> getActors()
	{
		ArrayList<Actor> actors = new ArrayList<Actor>();
		Location location = getLocation();
		for (int i = location.getRow() - 2; i <= location.getRow() + 2; i++)
		{
			for (int j = location.getCol() - 2; j <= location.getCol() + 2; j++)
			{
				Location templocation = new Location(i,j);
				if (getGrid().isValid(templocation))
				{
					Actor a = getGrid().get(templocation);
					if (a != null && a != this)
					{
						actors.add(a);
					}
				}
			}
		}
		return actors;
	}
}
